//
//  ZLExamResultModel.m
//  ZLExamination
//
//  Created by Zheng on 2017/9/12.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLExamResultModel.h"

@implementation ZLExamResultListModel

- (void)setAttributes:(NSDictionary *)dataDic {
    [super setAttributes:dataDic];
    
    self.x_idx = dataDic[@"id"];
    
    if ([self.the_answer isEqualToString:@""]) {
        self.typeOfAnswer = @"1";
    } else {
        if ([self.the_answer isEqualToString:self.co_answer]) {
            self.typeOfAnswer = @"2";
        } else {
            self.typeOfAnswer = @"3";
        }
    }
    
    NSArray *opArr = @[@"A",@"B",@"C",@"D"];
    
    self.optionTypes = [@[@"1",@"1",@"1",@"1"] mutableCopy];
    
    if (![self.the_answer isEqualToString:@""]) {
        
        if ([self.the_answer isEqualToString:self.co_answer]) {
            //答对

            NSArray *arr = [self.co_answer componentsSeparatedByString:@";"];

            [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSString *str = obj;
                [opArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([str isEqualToString:obj]) {
                        [self.optionTypes replaceObjectAtIndex:idx withObject:@"2"];
                    }
                }];
            }];
            
        } else {
            //答错
            //我的答案
            NSArray *arr = [self.the_answer componentsSeparatedByString:@";"];
            
            if (arr.count == 1) {
                //如果是单选
                [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    NSString *str = obj;
                    [opArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if ([str isEqualToString:obj]) {
                            [self.optionTypes replaceObjectAtIndex:idx withObject:@"3"];
                        }
                        
                        if ([self.co_answer isEqualToString:obj]) {
                            [self.optionTypes replaceObjectAtIndex:idx withObject:@"2"];
                        }
                    }];
                }];
            } else {
                //如果是多选
                                
                //我的错误答案
                NSArray *falseArr = [self.the_answer componentsSeparatedByString:@";"];
                //正确答案
                NSArray *trueArr = [self.co_answer componentsSeparatedByString:@";"];
                
                //我的错误答案集合
                NSMutableSet *falseSet = [NSMutableSet setWithArray:falseArr];
                //我的正确答案集合
                NSMutableSet *trueSet = [NSMutableSet setWithArray:trueArr];

                //获得错误答案中和正确答案相同的
                [falseSet intersectSet:trueSet];
                
                //错误答案中的正确答案
                NSArray *falseTrueArr = [falseSet allObjects];
                
                //错误-错误中正确的 错误答案
                NSMutableArray *newFalseArr = [falseArr mutableCopy];
                
                //排除正确的答案
                [falseTrueArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    NSString *str = obj;
                    [falseArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if ([str isEqualToString:obj]) {
                            [newFalseArr removeObject:str];
                        }
                    }];
                }];
                
                [newFalseArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    [opArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        NSString *str = obj;
                        if ([str isEqualToString:obj]) {
                            [self.optionTypes replaceObjectAtIndex:idx withObject:@"3"];
                        }
                    }];
                }];
                
                
                [trueArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    NSString *str = obj;
                    [opArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if ([str isEqualToString:obj]) {
                            [self.optionTypes replaceObjectAtIndex:idx withObject:@"2"];
                        }
                    }];
                }];
                
            }
        }
    } else {
        NSArray *arr = [self.co_answer componentsSeparatedByString:@";"];
        
        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString *str = obj;
            [opArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([str isEqualToString:obj]) {
                    [self.optionTypes replaceObjectAtIndex:idx withObject:@"2"];
                }
            }];
        }];
    }
    
    NSArray<NSString *> *arr = [self.op_answer componentsSeparatedByString:@";"];
    NSMutableArray *newArr = [NSMutableArray new];
    [arr enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *str = [obj substringFromIndex:2];
        [newArr addObject:str];
    }];
    
    self.options = [newArr copy];
    
}

@end

@implementation ZLExamResultModel

- (void)setAttributes:(NSDictionary *)dataDic {
    [super setAttributes:dataDic];
    
    NSMutableArray *singleModels = [NSMutableArray new];
    NSMutableArray *multipleModels = [NSMutableArray new];
    
    NSMutableArray *wrongSingleModels = [NSMutableArray new];
    NSMutableArray *wrongMultipleModels = [NSMutableArray new];
    
    self.x_id = dataDic[@"id"];
    
    [self.subject_lists enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        ZLExamResultListModel *model = [[ZLExamResultListModel alloc] initWithDataDic:obj];
        model.questionNumber = idx+1;
        if (model.type.integerValue == 1) {
            //单选
            [singleModels addObject:model];
            
            //错题
            if (model.typeOfAnswer.integerValue != 2) {
                [wrongSingleModels addObject:model];
            }
            
        } else {
            //多选
            [multipleModels addObject:model];

            //错题
            if (model.typeOfAnswer.integerValue != 2) {
                [wrongMultipleModels addObject:model];
            }
        }
        
    }];
    
    self.answers = @[singleModels,multipleModels];
    
    self.wrongAnswes = @[wrongSingleModels,wrongMultipleModels];
    
}

@end
